<template>
	<view class="content">
		<heade title="草稿列表" cross bg_color="bg_fff"></heade>
		<view class="position-fixed" :style="{'top':($common.headHeight() + 44) + 'px'}" style="width: 100%;z-index: 9;">
			<view class="d-flex align-center justify-content-space-around bg_fff" style="border-bottom: 2rpx solid #eee;">
				<view v-for="(item,i) in tabList" @tap="countClick(i)" class="text-center p-t10" style="width: 33.333%;">
					<view class="p-b10" :class="count == i?'co_4DAABF':''">{{item}}</view>
					<view v-if="count == i" class="biankuang"></view>
				</view>
			</view>
		</view>
		<view style="height: 82rpx;"></view>
		<view class="m-t5">
			<view v-for="(item,i) in arrList" @tap="urlLink(item)">
				<view class="d-flex align-center bg_fff m-t3 box_box" style="padding: 20rpx;">
					<view class="iconfont f-s16" @tap.stop="choiceClick(item,i)" :class="item.isStatus?'icon-danxuankuang-xuanzhong co_64acff':'icon-danxuankuang'"></view>
					<view class="flex1 m-l15 m-r20">
						<view class="d-flex align-center justify-content-space-between f-s14" v-if="item.dto">
							<view >{{item.dto.linkMan}}</view>
							<view>{{item.dto.voucherDate}}</view>
						</view>
						<view class="d-flex align-center justify-content-end f-s14 m-t8">
							<view class="co_FF0000">¥{{Number(item.toNumMoney).toFixed(2)}}</view>
						</view>
					</view>
					<view class="iconfont icon-shanchu co_FF0000 f-wbold" @tap.stop='deleteClick(i)'></view>
				</view>
			</view>
		</view>
		<view class="b-liuhai" ><view style="height: 180rpx;"></view></view>
		<view style="position: fixed; bottom: 0;width: 100%;z-index: 9;" class="bg_fff">
			<view class="b-liuhai">
				<view class="bg_3A8CFF d-flex align-center justify-content-center" style="height: 90rpx;" @tap="submit">
					<view class="co_fff f-s14 f-w400">提交</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabList:['销售订单','销货单','退货单'],
				count:0,
				arrList:[
					{isStatus:false},
					{isStatus:false},
					{isStatus:false},
				],
				saleList:[],
				salesList:[],
				returnGoods:[],
				resData:{},
				index:0
			}
		},
		onShow() {
			// 销售单
			let _this = this
			if(this.count == 0) {
				this.arrList =  this.$db.get('dindan')
			}else if(this.count == 1) {
				this.arrList = this.$db.get('dindan_2') // 销货单
			}else {
				this.arrList = this.$db.get('dindan_1') // 退货单
			}
			if(this.arrList.length > 0) {
				this.arrList.forEach(e => {
					e.isStatus = false
					e.toNumMoney = 0
					if(_this.count != 0) {
						e.dto.saleDeliveryDetails.forEach(j => {
							j.inventory = {
								code: j.Inventory.Code
							}
							j.unit = {
								name: j.Unit.Name
							}
							j.origTaxPrice = j.OrigTaxPrice
							j.quantity = j.Quantity
							e.toNumMoney =(Number(e.toNumMoney) + Number(j.origTaxPrice) * Number(j.quantity)).toFixed(2)
						})
					}else {
						e.dto.saleOrderDetails.forEach(j => {
							j.inventory = {
								code: j.Inventory.Code
							}
							j.unit = {
								name: j.Unit.Name
							}
							j.origTaxPrice = j.OrigTaxPrice
							j.quantity = j.Quantity
							e.toNumMoney =(Number(e.toNumMoney) + Number(j.origTaxPrice) * Number(j.quantity)).toFixed(2)
						})
					}
				})
			}
		},
		methods: {
			countClick(i) {
				let _this = this
				this.count = i
				if(this.count == 0) {
					this.arrList =  this.$db.get('dindan')
				}else if(this.count == 1) {
					this.arrList = this.$db.get('dindan_2') // 销货单
				}else {
					this.arrList = this.$db.get('dindan_1') // 退货单
				}
				if(this.arrList.length > 0) {
					this.arrList.forEach(e => {
						e.isStatus = false
						e.toNumMoney = 0
						if(_this.count != 0) {
							e.dto.saleDeliveryDetails.forEach(j => {
								j.inventory = {
									code: j.Inventory.Code
								}
								j.unit = {
									name: j.Unit.Name
								}
								j.origTaxPrice = j.OrigTaxPrice
								j.quantity = j.Quantity
								e.toNumMoney += (Number(j.OrigTaxPrice) * Number(j.Quantity))
							})
						}else {
							e.dto.SaleOrderDetails.forEach(j => {
								j.inventory = {
									code: j.Inventory.Code
								}
								j.unit = {
									name: j.Unit.Name
								}
								j.origTaxPrice = j.OrigTaxPrice
								j.quantity = j.Quantity
								e.toNumMoney += (Number(j.OrigTaxPrice) * Number(j.Quantity))
							})
						}
					})
				}
			},
			choiceClick(item,i) {
				this.arrList.forEach((e,index) => {
					if(i == index) {
						item.isStatus = true
						this.resData = e
						this.index = i
					}else {
						e.isStatus = false
					}
					this.$forceUpdate()
				})
			},
			// 删除
			deleteClick(i) {
				let _this = this
				uni.showModal({
					title: '删除',
					content: '是否确认删除',
					success: function (res) {
						if (res.confirm) {
							_this.arrList.forEach((e,index) => {
								if(index == i) {
									_this.arrList.splice(i,1)
								}
							})
							if(_this.count == 0) {
								if(_this.arrList.length == 0) {
									_this.$db.del('dindan')
								}else {
									_this.$db.set('dindan',_this.arrList)
								}
							}else if(_this.count == 1) {
								if(_this.arrList.length == 0) {
									_this.$db.del('dindan_2')
								}else {
									_this.$db.set('dindan_2',_this.arrList)
								}
							}else {
								if(_this.arrList.length == 0) {
									_this.$db.del('dindan_1')
								}else {
									_this.$db.set('dindan_1',_this.arrList)
								}
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 详情跳转
			urlLink(item) {
				let _this = this
				if(this.count != 0) {
					uni.navigateTo({
						url: `/pages/aDraftList/details?SaleOrderDetails`,
						success: function(res) {
							setTimeout(() => {
								uni.$emit('page-popup', {
									content: item.dto,
									count:_this.count,
									externalCode:item.dto.externalCode
								})
							}, 500)
						}
					})
				}else {
					uni.navigateTo({
						url: `/pages/aDraftList/details?SaleOrderDetails`,
						success: function(res) {
							setTimeout(() => {
								uni.$emit('page-popup', {
									content: item.dto,
									count:_this.count,
									externalCode:item.dto.externalCode
								})
							}, 500)
						}
					})
				}
			},
			// 提交订单
			submit() {
				let _this = this
				let num = 0
				if(this.arrList.length > 0) {
					this.arrList.forEach((e,index) => {
						if(e.isStatus == true) {
							num++
						}
					})
				}
				if(num == 0) {
					return this.$common.show('请选择要提交的订单',true)
				}
				this.arrList.forEach((e,index) => {
					if(e.isStatus == false) {
						num += 1
					}
				})
				let url = ''
				if(this.count == 0) {
					url = 'ApiSA/SaleOrderCreate'
				}else {
					url = 'ApiSA/SaleDeliveryCreate'
				}
				this.$http.post(url,this.resData).then(res => {
					console.log(res)
					let reDa = JSON.parse(res.data)
					if(reDa == null) {
						this.$common.show('操作成功')
						_this.arrList.forEach((e,index) => {
							if(index == _this.index) {
								_this.arrList.splice(_this.index,1)
							}
						})
						if(_this.count == 0) {
							if(_this.arrList.length == 0) {
								_this.$db.del('dindan')
							}else {
								_this.$db.set('dindan',_this.arrList)
								this.arrList =  this.$db.get('dindan')
							}
						}else if(_this.count == 1) {
							if(_this.arrList.length == 0) {
								_this.$db.del('dindan_2')
							}else {
								_this.$db.set('dindan_2',_this.arrList)
								this.arrList = this.$db.get('dindan_2') // 销货单
							}
						}else {
							if(_this.arrList.length == 0) {
								_this.$db.del('dindan_1')
							}else {
								_this.$db.set('dindan_1',_this.arrList)
								this.arrList = this.$db.get('dindan_1') // 退货单
							}
						}
					}else {
						this.$common.show(reDa.RestException.message,true)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #eeee;
	}
</style>